package com.wanglibao.woodtrade.woodtrade.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.entity.SupplyEntity;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.ui.activity.DemandActivity;
import com.wanglibao.woodtrade.woodtrade.ui.activity.SupplyAndDemendActivity;
import com.wanglibao.woodtrade.woodtrade.ui.activity.adapter.ItemLvSupplyAndDemendAdapter;
import com.wanglibao.woodtrade.woodtrade.util.PullUtil;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshListView;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshBase;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lugg on 2016/5/25.
 */
public class SupplyByTypeFragment extends BaseFragment  implements AdapterView.OnItemClickListener, PullToRefreshListView.OnRefreshListener2 {

    int pageIndex = 1;
    @Bind(R.id.list_fragment_supply_and_type_data)
    PullToRefreshListView list_fragment_supply_and_type_data;

    @Bind(R.id.ll_fragment_supply_and_type_null)
    LinearLayout ll_fragment_supply_and_type_null;

    SupplyEntity supplyEntity;

    public String keyWord = "";
    public String category_id = "";
    public String category_one_id = "";
    public String woodTypeDetailThreePlaceId = "";

    ItemLvSupplyAndDemendAdapter itemLvSupplyAndDemendAdapter;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.arg2 == 2) {
                if (pageIndex == 1){
                    if (supplyEntity != null && supplyEntity.getList() != null){
                        supplyEntity.getList().clear();
                    }
                    itemLvSupplyAndDemendAdapter.update(supplyEntity.getList());
                    if (ll_fragment_supply_and_type_null.getVisibility() == View.GONE){
                        ll_fragment_supply_and_type_null.setVisibility(View.VISIBLE);
                        list_fragment_supply_and_type_data.setVisibility(View.GONE);
                    }
                }else{
                    ToastUtil.createNormalToast("没有更多数据了");
                    pageIndex--;
                }
            }else{
                SupplyEntity supplyEntity1 = new Gson().fromJson(msg.obj.toString(), SupplyEntity.class);
                if (pageIndex == 1){
                    supplyEntity = supplyEntity1;
                }else{
                    supplyEntity.getList().addAll(supplyEntity1.getList());
                }
                itemLvSupplyAndDemendAdapter.update(supplyEntity.getList());
                if (ll_fragment_supply_and_type_null.getVisibility() == View.VISIBLE){
                    ll_fragment_supply_and_type_null.setVisibility(View.GONE);
                    list_fragment_supply_and_type_data.setVisibility(View.VISIBLE);
                }
            }

            list_fragment_supply_and_type_data.onRefreshComplete();
            return false;
        }
    });
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_supply_and_type, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(getUserVisibleHint()&&isVisible()){
            loadData();
        }
    }

    private void initView() {
        category_one_id = getArguments().getString("woodTypeId");


        supplyEntity = new SupplyEntity();
        supplyEntity.setList(new ArrayList<SupplyEntity.ListEntity>());
        itemLvSupplyAndDemendAdapter = new ItemLvSupplyAndDemendAdapter(getContext(),supplyEntity.getList());
        list_fragment_supply_and_type_data.getRefreshableView().setAdapter(itemLvSupplyAndDemendAdapter);
        list_fragment_supply_and_type_data.getRefreshableView().setOnItemClickListener(this);
        list_fragment_supply_and_type_data.setMode(PullToRefreshBase.Mode.BOTH);
        list_fragment_supply_and_type_data.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));
        list_fragment_supply_and_type_data.setOnRefreshListener(this);
        PullUtil.initIndicator(list_fragment_supply_and_type_data);
    }

    public void loadData() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("type",1);
            jsonObject.put("category_one",category_one_id);
            jsonObject.put("category",category_id);
            jsonObject.put("num",0);
            jsonObject.put("title",keyWord);
            jsonObject.put("page",pageIndex);
            jsonObject.put("place_id",woodTypeDetailThreePlaceId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(false,this, NetConfig.SERVER + NetConfig.TYPE_MESSAGE,
                NetConfig.messageSendList, handler,
                "jsonObject", NetConfig.TaskId.messageSendList,
                new Object[]{jsonObject});
        jsonRPCAsyncTask.execute(new Void[]{});
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), DemandActivity.class);
        intent.putExtra("ID",supplyEntity.getList().get(position-1).getId());
        startActivity(intent);
    }

    @OnClick(R.id.tv_layout_null_sentInfo) void sentInfoClick(){
        startActivity(new Intent(getActivity(), SupplyAndDemendActivity.class));
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        pageIndex = 1;
        loadData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        pageIndex++;
        loadData();
    }
}
